<?php


if(isset($_GET['add'])){
  $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
  while($quantity_row=mysql_fetch_assoc($quantity)){
    if($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]){
         $_SESSION['cart_'.(int)$_GET['add']]+='1';
      }  
    }
    header('Location: '.$page);
       
  }
  
if(isset($_GET['remove']))
{
    $_SESSION['cart_'.(int)$_GET['remove']]--;
}

if(isset($_GET['delete']))
{
   $_SESSION['cart_'.(int)$_GET['delete']]='0';
}


function products()
{
	$get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC');
	if(mysql_num_rows($get)==0)
	{
		;
	}
	else
	{
		while($get_row=mysql_fetch_assoc($get))
		{
			echo '<p>'.$get_row['name'].'<br />'.number_format($get_row['Product_id'],2).'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
		}
	}
}

function cart(){
  foreach($_SESSION as $name => $value){
    if($value>0){
        if(substr($name, 0, 5)=='cart_') {
           $id=substr($name, 5, (strlen($name)-5));
           $get=mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
          while($get_row=mysql_fetch_assoc($get)){
             $sub= $get_row['price']*$value;
             echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'],2).' = $'.number_format($sub,2).'<a href="cart.php?remove='.$id.'">[-] </a><a href="cart.php?add='.$id.'"> [+] </a><a href="cart.php?delete='.$id.'">[del] </a><br/>';
          }
        }
        $total+=$sub;
     }  
    }if($total==0){
      echo "Your shopping cart is empty.";
  } else{
    echo '$'.number_format($total,2).'<br />';
     ?>

<img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" align="left" style="margin-right:7px;">
     
    <?php
}
}




?>
